package com.myx.filter;

import com.myx.pojo.User;
import com.myx.tool.Constant;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "VipFilter",urlPatterns = {
        "/jsp/pages/vip/*"})
public class VipFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest= (HttpServletRequest) request;
        HttpServletResponse httpServletResponse= (HttpServletResponse) response;
        HttpSession session = httpServletRequest.getSession();
        User loginUser = (User) session.getAttribute(Constant.LOGIN_SESSION);
        if (loginUser == null) {
            //未登录
            httpServletRequest.getRequestDispatcher("/index.jsp").forward(httpServletRequest,httpServletResponse);
        }else {
            //已登录
            System.out.println("通过");
            //控制条件
            if (loginUser.getUserType() == 1) {

            }else if(loginUser.getUserType() == 0){

            }else {

            }

        }

        chain.doFilter(request, response);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
